/*
 * 分享成功之后调用的方法
 * @param
 * shareType----分享类型，0代表分享给朋友，1代表分享到朋友圈
 * shareSource--分享来源
 * shareId------分享ID
 */
function shareRecord(shareType, shareSource, shareId) {
    var localURL = window.location.href;
    $.ajax({
        url: globalPath + "/v1/lenovo/weChat/shareRecord.do",
        dataType: "json",
        type: "post",
        data: {
            shareType: shareType,
            shareUrl: localURL,
            shareId: shareId,
            shareSource: shareSource
        },
        success: function (data) {
            if (data.values != '') {
                _argumentsObj.playTimes = data.values.remainDrawNumber;
                _argumentsObj.shareCount = data.values.shareNumber;
            }
        },
        error: function () {
            //console.log("服务器异常");
            //如果此处无响应8.4版本应该跳转到车主认证页面
        }
    })
}



//更新验证码图片
function changeImg(id) {
    var imgSrc = $(imgCode);
    var src = "http://yhq.lenovo.com.cn/v1/anon/user/create/verifyCode.do";
    imgSrc.attr("src", chgUrl(src));
}
/*
 * 时间戳    
 *为了使每次生成图片不一致，即不让浏览器读缓存，所以需要加上时间戳 
 *
 */
function chgUrl(url) {
    var timestamp = (new Date()).valueOf();
    var urlurl = url.substring(0, 14);
    if ((url.indexOf("&") >= 0)) {
        urlurl = url + "×tamp=" + timestamp;
    } else {
        urlurl = url + "?timestamp=" + timestamp;
    }
    return urlurl;
}
/*
 * 获取短信验证码方法
 */

var globalPath = "http://yhq.lenovo.com.cn";
var checkGetVeryCode = true;
var seconds = null;

function getVerCode() {
    //记录点击事件
    clickListener(globalVar.source, "h1-109", "获取短信验证码");
    if (!checkGetVeryCode) {
        return;
    }
    var phone = document.getElementById("phoneNumber").value;
    var imgVeryCode = $("#testCodeInp").val();
    console.log(phone, imgVeryCode)
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1})|(19[0-9]{1}))+\d{8})$/;
    if (!myreg.test(phone) || phone == "") {
        console.log(111);
        $(".errorTip").show();
        $(".errorTip .errorTipWord").show();
        $(".errorTip.errorTipWord").html("请填写真实且准确的手机号。");
        return
    }
    if (imgVeryCode == "") {
        $(".errorTip").show();
        $(".errorTip .errorTipWord").show();
        $(".errorTip .errorTipWord").html("请输入图形验证码");
        return
    }
    checkGetVeryCode = false;
    $.ajax({
        url: globalPath + "/v1/lenovo/activate/shortMessage.do",
        dataType: "jsonp",
        type: "post",
        data: {
            phone: phone,
            imgVeryCode: imgVeryCode
        },
        success: function (data) {
            if (data.result == "ok") {
                $(".errorTip").show();
                $(".errorTip .errorTipWord").show();
                $(".errorTip .errorTipWord").html("短信验证码发送成功!");
                $("#getVeryCode").html("60s");
                seconds = setInterval(function () {
                    var currentSecond = parseInt($("#getVeryCode").html());
                    if (currentSecond == 0) {
                        $("#getVeryCode").html("获取");
                        clearInterval(seconds);
                        checkGetVeryCode = true;
                    } else {
                        $("#getVeryCode").html((currentSecond - 1) + "s");
                    }
                }, 1000);
            } else {
                $(".errorTip").show();
                $(".errorTip .errorTipWord").show();
                $(".errorTip .errorTipWord").html(data.errormsg);
                changeImg();
                checkGetVeryCode = true;
            }

        }
    });
};
//信息录入成功后
function checkInfo() {
    var phone = document.getElementById("phoneNumber").value;
    var veryCode = document.getElementById("veryCode").value;
    var checkFalse = [];
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
    var countFale = 0;
    var tempTip = "";
    var errorTip = ['姓名', '手机号', '验证码'];
    if (!myreg.test(phone) || phone == "") {
        tempTip = "请填写真实且准确的手机号码。";
        countFale++;
        checkFalse.push(1);
    }
    if (veryCode == "") {
        tempTip = "请填写验证码。";
        countFale++;
        checkFalse.push(2);
    }
    if (countFale == 1) {
        return false;
    } else if (countFale > 1) {
        tempTip = "";
        $.each(checkFalse, function (i, p) {
            if (i != checkFalse.length - 2) {
                tempTip += errorTip[p] + "、";
            } else {
                tempTip += errorTip[p] + "及";
            }
        });
        tempTip = tempTip.substring(0, tempTip.length - 1);
        tempTip += "有误。";
        $(".errorTip").show();
        $(".errorTip .errorTipWord").show();
        $(".errorTip .errorTipWord").html(tempTip);
        return false;
    }

    var ulr = "";
    var dataArgument = {
        phone: phone,
        code: veryCode,
    };

    return dataArgument;
}

//关闭录入手机号弹窗
function closeAlert() {
    $("#phone").val("");
    $("#imgVeryCodeInp").val("");
    $("#veryCode").val("");
    $(".infoAlert").hide();
    $('body').removeClass("body-fixed");
}
//关闭错误提示弹窗
function closeErrorTip(el) {
    if (_argumentsObj.followStatus == 0 && _argumentsObj.sourcePage == 0) {
        window.location.reload();
    }
    if ($(el).attr("data-canShare") == 1) {
        $(".indexShare").show();
        $(el).attr("data-canShare", 0)
    }
    $(".errorTip").hide();
    $(".errorTipContent>div").hide();
}

//根据sorce变更二维码
function checkAttention(source) {
    var imgUrl = {
        '50': '70661',
        '51': '70662',
        '52': '70663',
        '53': '70664',
        '54': '70665',
        '55': '70666',
        '56': '70667',
        '57': '70668',
        '58': '70669',
        '59': '70670',
        '60': '70671',
        '61': '70672',
        '62': '70673',
        '63': '70674',
        '64': '70675',
        '65': '70676',
        '66': '70677',
        '67': '70678',
        '68': '70679'
    };
    var qCodeUrl = imgUrl[source];
   if (qCodeUrl == undefined) {
        qCodeUrl = imgUrl['64'];
    } 
    var tempHtml = "img/code/" + qCodeUrl + ".jpg";
    console.log(tempHtml);
    $(".QRcode").attr('src', tempHtml);
}

//根据sorce变更链接url
function checkUrl(source) {
        var arr2 = {
            "50": "https://shop.lenovo.com.cn/activity/marketing/opening/index.html",
            "51": "https://shop.lenovo.com.cn/activity/marketing/opening/pczol/index.html",
            "52": "https://shop.lenovo.com.cn/activity/marketing/opening/pcsem/index.html",
            "53": "https://shop.lenovo.com.cn/activity/marketing/opening/pcppzq/index.html",
            "54": "https://shop.lenovo.com.cn/activity/marketing/opening/pcqt1/index.html",
            "55": "https://m.lenovo.com.cn/android/activity/marketing/opening/index.html",
            "56": "https://m.lenovo.com.cn/activity/marketing/opening/index.html",
            "57": "https://m.lenovo.com.cn/activity/marketing/opening/wapcjkc/index.html",
            "58": "https://m.lenovo.com.cn/activity/marketing/opening/wapyl/index.html",
            "59": "https://m.lenovo.com.cn/activity/marketing/opening/wapyk/index.html",
            "60": "https://m.lenovo.com.cn/activity/marketing/opening/wapzyz/index.html",
            "61": "https://m.lenovo.com.cn/activity/marketing/opening/wapgoogle/index.html",
            "62": "https://m.lenovo.com.cn/activity/marketing/opening/wapsem/index.html",
            "63": "https://m.lenovo.com.cn/activity/marketing/opening/wapppzq/index.html",
            "64": "https://m.lenovo.com.cn/activity/marketing/opening/wapsocial/index.html",
            "65": "https://m.lenovo.com.cn/activity/marketing/opening/wapqt1/index.html",
            "66": "https://m.lenovo.com.cn/activity/marketing/opening/wapqt2/index.html",
            "67": "https://m.lenovo.com.cn/activity/marketing/opening/wapqt2/index.html",
            "68": "https://m.lenovo.com.cn/activity/marketing/opening/wapqt2/index.html"
        };
        var url = arr2[source];
        if (url == undefined) {
             url = arr2['54'];
         } 

    return url;
}

/*
 * 点击事件的公用方法
 * @param
 * source------来源
 * clickCode---事件编号
 * clickName---事件名称
 * 
 */
function clickListener(source, clickCode, clickName) {
    $.ajax({
        url: globalVar.path + "/v1/lenovo/wintersale/onclick.do",
        dataType: "jsonp",
        data: {
            source: source,
            eventCode: clickCode,
            eventName: clickName
        },
        success:function (data) {
            //console.log(data.errormsg);
        }
    });
}